import Vue from 'vue'

import 'normalize.css/normalize.css' // a modern alternative to CSS resets

import Element, { Loading } from 'element-ui'

import './styles/element-variables.scss'

import '@/styles/index.scss' // global css



import App from './App'
import store from './store'
import router from './router'

import './icons' // icon
import './permission' // permission control


import { VueJsonp } from 'vue-jsonp'
Vue.use(VueJsonp)
const qs = require('qs')
const api = {
  async get(url, data) {
    try {
      let res = await axios.get(url, { params: data })
      res = res.data
      return new Promise((resolve) => {
        if (res.code === 0) {
          resolve(res)
        } else {
          resolve(res)
        }
      })
    } catch (err) {
      alert('服务器出错')
      console.log(err)
    }
  },
  async post(url, data) {
    try {
      let res = await axios.post(url, qs.stringify(data))
      res = res.data
      return new Promise((resolve, reject) => {
        if (res.code === 0) {
          resolve(res)
        } else {
          reject(res)
        }
      })
    } catch (err) {
      // return (e.message)
      alert('服务器出错')
      console.log(err)
    }
  },
}
export { api }

//请求数据拦截器
axios.interceptors.request.use(request => {
  startLoading();
  return request
}, err => {
  return Promise.reject(err);
});



let loading;
//内存中正在请求的数量
let loadingNum = 0;
function startLoading() {
  if (loadingNum == 0) {
    loading = Loading.service({
      lock: true,
      text: "拼命加载中...",
      background: 'rgba(0,0,0,0.7)'
    })
  }
  //请求数量加1
  loadingNum++;
}
function endLoading() {
  //请求数量减1
  loadingNum--
  if (loadingNum <= 0) {
    loading.close()
  }
}

//接收响应拦截器
axios.interceptors.response.use(response => {
  endLoading();
  return response
}, err => {
  endLoading();
  if (err && err.response) {
    switch (err.response.status) {
      case 400: err.message = '请求错误(400)'; break;
      case 401: this.$router.push('/login'); break;
      case 403: err.message = '拒绝访问(403)'; break;
      case 404: err.message = '请求出错(404)'; break;
      case 408: err.message = '请求超时(408)'; break;
      case 500: err.message = '服务器错误(500)'; break;
      case 501: err.message = '服务未实现(501)'; break;
      case 502: err.message = '网络错误(502)'; break;
      case 503: err.message = '服务不可用(503)'; break;
      case 504: err.message = '网络超时(504)'; break;
      case 505: err.message = 'HTTP版本不受支持(505)'; break;
      default: err.message = `连接出错(${err.response.status})!`;
    }
  } else {
    err.message = '连接服务器失败!'
  }
  Message.error(err.message);
  return Promise.reject(err);
});
import { Message } from 'element-ui';




import { title } from './settings.js'

// element-ui 全局组件样式
Vue.use(Element, {
  size: 'mini'
})
Vue.config.productionTip = false

// View UI
import ViewUI from 'view-design';
import 'view-design/dist/styles/iview.css';
Vue.use(ViewUI);

// 全局弹窗样式
import '@/sa-frame/kj/layer/theme/default/layer.css';
import layer from '@/sa-frame/kj/layer/layer';
global.layer = layer;
Vue.prototype.layer = layer;


import AMap from 'vue-amap'
Vue.use(AMap)
AMap.initAMapApiLoader({
  key: '107b7f4763513ea26105a82ea6820c5c',
  plugin: ['AMap.Autocomplete', 'AMap.PlaceSearch', 'AMap.Scale', 'AMap.OverView', 'AMap.ToolBar', 'AMap.MapType', 'AMap.PolyEditor', 'AMap.CircleEditor'],
  // 默认高德 sdk 版本为 1.4.4
  v: '1.4.4'
})


// 全局sa对象
import '@/sa-frame/sa.css'
import sa from '@/sa-frame/sa.js'
global.sa = sa;
Vue.prototype.sa = sa

// 全局表单组件
import SaItem from '@/sa-frame/com/sa-item.vue'
import SaTd from '@/sa-frame/com/sa-td.vue'
import SaInfo from '@/sa-frame/com/sa-info.vue'
Vue.component('sa-item', SaItem)
Vue.component('sa-td', SaTd)
Vue.component('sa-info', SaInfo)

// 文件上传工具方法封装
import '@/sa-frame/kj/upload-util.js';
import axios from "axios";

import * as echarts from 'echarts'
import china from 'echarts/map/json/china.json';
echarts.registerMap('china', china);
Vue.prototype.$echarts = echarts;

new Vue({
  el: '#app',
  data: {
    title: title
  },
  router,
  store,
  render: h => h(App)
})

import moment from 'moment'
Vue.prototype.$moment = moment;
